package com.fangs.lunbotu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.fangs.lunbotu.banner.BannerViewAdapter;
import com.fangs.lunbotu.banner.BaseBanner;
import com.fangs.lunbotu.banner.BannerPage;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private BaseBanner homeBanner;
    private RecyclerView rlPoint;
    private PointQuickAdapter pointQuickAdapter;
    private List<BannerPage> bannerList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rlPoint = findViewById(R.id.rl_point);
        homeBanner = findViewById(R.id.home_banner);

        for (int i = 0; i < 3; i++) {
            bannerList.add(new BannerPage("https://gossv.cfp.cn/videos/mts_videos/medium/VCG2216830485.mp4", "https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp"));
        }

//        bannerList.add(new BannerPage("https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp", 3000));
//        bannerList.add(new BannerPage("https://img.ltwebstatic.com/images3_ach/2022/06/10/1654861945d4e2fdf787b3001b704e1460588ae75e.webp", 3000));

        pointQuickAdapter = new PointQuickAdapter();

        homeBanner.setDataPlay(bannerList);
        rlPoint.scheduleLayoutAnimation();

        //如果只有一张图或者一个视频，point则隐藏
        if (bannerList.size() == 1) {
            rlPoint.setVisibility(View.GONE);
        }

        pointQuickAdapter.setNewData(bannerList);
        rlPoint.setNestedScrollingEnabled(true);
        rlPoint.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rlPoint.setAdapter(pointQuickAdapter);

        //点击事件
        homeBanner.setOnClicked(new BaseBanner.OnClicked() {
            @Override
            public void onItemClicked(int positions) {
                Log.e("——？？？？——", "" + positions);
            }
        });


        homeBanner.setPageChangeListener(position -> {
            if (pointQuickAdapter.getData().isEmpty()) {
                return;
            }
            pointQuickAdapter.setSelectedPosition(position);
            pointQuickAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        homeBanner.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        homeBanner.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        homeBanner.destroy();
    }
}